<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Wallet;
use App\Models\Currency;
use App\Library\ETH\EthereumFunction;

class CreateEthWallet extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * 创建新的eth系统钱包
     * @return mixed
     */
    public function handle()
    {
        //eth钱包
        $cid = Currency::where('name_en', 'ETH')->value('id');
        $unlocks = Wallet::where(['status' => 0, 'cid' => $cid])->count();
        //未锁定钱包少于3个则创建新的系统钱包
        if ($unlocks < 3) {
            $btc = new EthereumFunction();
            $res = $btc->createWalletAddress();
            if ($res) {
                Wallet::create(
                    [
                        'cid' => $cid,
                        'address' => $res,
                        'balance' => 0,
                        'block_balance' => 0,
                        'grand_recharge' => 0,
                        'grand_withdraw' => 0,
                        'grand_proc' => 0,
                        'status' => 0,
                    ]
                );
            }
        }
    }
}
